package top.kaoshanji.t1ch11a;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;

/**
 * @author kaoshanji
 * ^_^
 * create time 2025/5/1 14:27
 */
@SpringBootTest
@AutoConfigureMockMvc
public class PaymentsControllerTest {

    @Autowired
    private MockMvc mockMvc;

    /**
     * 代码清单test-t1ch11a-01：一个API接口测试示例
     * @throws Exception
     */
    @Test
    public void testCreatePayment() throws Exception {
        Payment requestBody = new Payment();
        requestBody.setAmount(1000);

        ObjectMapper mapper = new ObjectMapper();

        mockMvc.perform(
                MockMvcRequestBuilders.post("/createPayment").header("requestId", "121212")
                        .content(mapper.writeValueAsString(requestBody)).contentType(MediaType.APPLICATION_JSON))
                        .andExpect(MockMvcResultMatchers.status().isOk())
                        .andExpect(MockMvcResultMatchers.jsonPath("$.amount").value(1000));
    }


}
